/*
 * HashBag.java
 * 
 * Created on Jun 6, 2007, 2:14:24 AM
 * 
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.infomancers.collections;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 *
 * @author freakazoid
 */
public class HashBag<T, K> implements Bag<T, K> {

    private Map<T, Collection<K>> bag = new HashMap<T, Collection<K>>();
    
    public boolean add(T key, K value) {
        Collection<K> items = bag.get(key);
        
        if (items == null) {
            items = new LinkedList<K>();
            bag.put(key, items);
        }
        
        return items.add(value);
    }

    public boolean remove(T key) {
        return bag.remove(key) != null;
    }

    public boolean removeItem(T key, K value) {
        Collection<K> items = bag.get(key);
        
        return items != null ? items.remove(value) : false;
    }

    public Collection<K> getItems(T key) {
        return bag.get(key);
    }
}
